import registnet.dao.DegreeCoursePeer;
import registnet.dao.DegreeCourse;
import registnet.dao.CollegePeer;
import registnet.dao.College;
import resources.Constants;

DegreeCoursePeer degreeCoursePeer = new DegreeCoursePeer();
DegreeCourse degreeCourse;

List data = new Vector();
List resultSet = new Vector();

boolean isShowingInfo;
boolean isSearching;


/* [start] 	initializes gui variables */

/* [end] */

/* [start]	prepares the tree */
	ArrayList parentList = new ArrayList();
	Map mapSTN_parent = new HashMap();
	Map mapAL_parent = new HashMap();
	Map mapAL_child = new HashMap();
	Map map_treeitem = new HashMap();
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
/* [end] */


/* [start]	defines the renderer for the tree */
public class MyTreeitemRenderer implements TreeitemRenderer {
	public void render(Treeitem item, Object data) throws Exception {
		String str = data.toString();
		Treerow tr = null;
		Treecell tc = new Treecell(str);
		if(item.getTreerow()==null){
			tr = new Treerow();
			tr.setParent(item);
			map_treeitem.put(str, item);
		}else{
			tr = item.getTreerow(); 
			tr.getChildren().clear();
		}
		tc.setParent(tr);
	}
}
/* [end] */


/* [start]	defines an event listener for the items on the tree;
			when the item is selected, its info is displayed */
	private class SelectEventListener implements EventListener {	
		public SelectEventListener (){}
		
		public void onEvent(Event event) throws UiException {
			showInfo();
		}
	}	
/* [end] */


/* [start] displays the info of the selected item */
	void showInfo(){
		isShowingInfo = true;
		isSearching = false;
		try{
			if (instance_tree.getSelectedCount() == 1){
				if (instance_tree.getSelectedItem().getParentItem() != null){
					degreeCourse = degreeCoursePeer.getDegreeCourse(instance_tree.getSelectedItem().getLabel());
					loadContents(degreeCourse);
				}
				else
					title.setValue(title.getValue());	
			}
		}catch (Exception e) { }
	}
/* [end] */


/* [start]	retrieves the degree courses and places them on the tree;
			limits the degree courses to retrieve depending on the user type */
	public void retrieveObjects(){
		map_treeitem.clear();
		mapSTN_parent.clear();
		mapAL_parent.clear();
		mapAL_child.clear();
		parentList.clear();
		
		List colleges = new Vector();
		colleges = new CollegePeer().getAllColleges();
		
		for(int i=0; i<colleges.size(); i++){
			List degrees = degreeCoursePeer.getDegreeCourseByCollege(colleges.get(i).getCollegeId());
			ArrayList alChild = new ArrayList();
			ArrayList alLeaf = new ArrayList();
			for(int j=0; j<degrees.size(); j++){
				SimpleTreeNode stnChild = new SimpleTreeNode(degrees.get(j).getDegreeCode(), new ArrayList());
				alLeaf.add(degrees.get(j).getDegreeCode());
				alChild.add(stnChild);
			}
			SimpleTreeNode stnParent = new SimpleTreeNode(colleges.get(i).getCollegeCode(), alChild);
			mapSTN_parent.put(colleges.get(i).getCollegeCode(), stnParent);
			mapAL_parent.put(colleges.get(i).getCollegeCode(), alChild);
			mapAL_child.put(colleges.get(i).getCollegeCode(), alLeaf);
			parentList.add(stnParent);
		}
		
		SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
		SimpleTreeModel stm = new SimpleTreeModel(root);
		instance_tree.setModel(stm);
		instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
	}
/* [end] */


/* [start]	summary renderer */
private class SummaryRenderer implements ListitemRenderer  {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data[0]).setParent(item);
		new Listcell(data[1]).setParent(item);
		new Listcell(data[2]).setParent(item);
		new Listcell(data[3]).setParent(item);
	}
}
/* [end] */


/* [start] load contents of table */
	void loadContents(DegreeCourse degreeCourse){
		registnet.dao.StudentPeer daoStudentPeer = new registnet.dao.StudentPeer();
		List listDaoStudents = daoStudentPeer.getStudentsByDegree(degreeCourse.getDegreeId());
		ListIterator li = listDaoStudents.listIterator();
		String[][] listModel = new String[listDaoStudents.size()][4];
		
		int ctr=0;
		while(li.hasNext()){
			registnet.dao.Student daoStudent;
			try{
				daoStudent = (registnet.dao.Student)li.next();
				listModel[ctr][0] = daoStudent.getStudentNumber();
				listModel[ctr][1] = daoStudent.getFirstName();
				listModel[ctr][2] = daoStudent.getLastName();
				listModel[ctr][3] = Constants.MAP_STUDENT_STATUS_KEY.get(daoStudent.getStatus());
			}catch(Exception e){
				System.err.println(e.getMessage());
				continue;
			}
			ctr++;
		}
		ListModel strset = new SimpleListModel(listModel);
		contents.getFellow("listbox_content").setModel(strset);
		contents.getFellow("listbox_content").setItemRenderer(new SummaryRenderer());
	}
/* [end] */


/* [start]  updates the students' status*/
	void process(int type){
		if(contents.getFellow("listbox_content").getItemCount()==0) return;
		Set setItems = contents.getFellow("listbox_content").getSelectedItems();
		List studentNumberList = new Vector();
		
		Iterator ite = setItems.iterator();
		while(ite.hasNext()){
			Listitem listitem = ite.next();
			studentNumberList.add(listitem.getFirstChild().getLabel());
		}
		
		ite = studentNumberList.iterator();
		while(ite.hasNext()){
			String studentNumber = ite.next();
			new registnet.dao.StudentPeer().updateStudent(new registnet.dao.StudentPeer().getStudentId(studentNumber), type);
		}
		loadContents(degreeCourse);
	}
/* [end] */







